require('./payment.css');
require('page/common/nav/nav.js');
require('page/common/header/header.js');

var _common_util  = require('util/common_util.js');
var _payment_service        = require('service/payment-service.js');
var templateIndex   = require('./payment.string');

// page 逻辑部分
var page = {
    data: {
        orderNumber : _common_util.getURLParam('orderNumber')
    },
    init: function(){
        this.onLoad();
    },
    onLoad : function(){
        // 加载detail数据
        this.loadPaymentInfo();
    },
    // 加载订单列表
    loadPaymentInfo: function(){
        var _this           = this,
            paymentHtml     = '',
            $pageWrap       = $('.page-wrap');
        $pageWrap.html('<div class="loading"></div>');
        _payment_service.getPaymentInfo(this.data.orderNumber, function(res){
            // 渲染html
            paymentHtml = _common_util.renderHTML(templateIndex, res);
            $pageWrap.html(paymentHtml);
            _this.listenOrderStatus();
        }, function(errMsg){
            $pageWrap.html('<p class="err-tip">' + errMsg + '</p>');
        });
    },
    // 监听订单状态
    listenOrderStatus : function(){
        var _this = this;
        this.paymentTimer = window.setInterval(function(){
            _payment_service.getPaymentStatus(_this.data.orderNumber, function(res){
                if(res == true){
                    window.location.href 
                        = './result.html?type=payment&orderNumber=' + _this.data.orderNumber;
                }
            });
        }, 5e3);
    }
};
$(function(){
    page.init();
});